package com.ikun.controller;

import com.ikun.pojo.Result;
import com.ikun.pojo.TbTask;
import com.ikun.pojo.TbUser;
import com.ikun.pojo.TbUserTask;
import com.ikun.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TaskController {

    @Autowired
    TaskService taskService;

    //分页查询任务的方法
    @RequestMapping("pgSelTask")
    @ResponseBody
    public List<TbTask> pgSelTask(String pg,String order,String by) {
//        int page = Integer.parseInt(request.getParameter("pg"));
//        String order = request.getParameter("order");
//        String by = request.getParameter("by");
        System.out.println(pg+order+by);
        List<TbTask> tbTaskList = taskService.pgSelTask(Integer.parseInt(pg),order,by);
        return tbTaskList;
    }
    //用户发布任务的方法
    @RequestMapping("send_task")
    @ResponseBody
    public Result send_task(HttpServletRequest request, HttpServletResponse response) {
        String content = request.getParameter("data");
        String c = content.replace("\"","'");
        String title = request.getParameter("title");
        int cardnum = Integer.parseInt(request.getParameter("cardnum"));
        int usernum = Integer.parseInt(request.getParameter("usernum"));
        Long typeId = Long.parseLong(request.getParameter("type"));
        Long userid = Long.parseLong(request.getParameter("userid"));

        int count = taskService.send_task(title,typeId,c,userid,cardnum,usernum);
        if(count>0){
            Result result = new Result();
            result.setCode(0);
            return result;
        }
        return null;
    }

    //查询我发布的任务的方法
    @RequestMapping("selMyTask")
    @ResponseBody
    public Result selMyTask(HttpServletRequest request, HttpServletResponse response){
        int userid = Integer.parseInt(request.getParameter("id"));
        int page = Integer.parseInt(request.getParameter("pg"));
        List<TbTask> taskList = taskService.selMyTask(userid,page);
        int count = taskService.selMyTaskCount(userid);
        Result result = new Result();
        if(taskList==null || taskList.size()==0){
            result.setMessage("fail");
        }else{
            result.setMessage("success");
        }
        result.setList(taskList);
        result.setCode(count);
        return result;
    }

    //查询我参与的任务的方法
    @RequestMapping("selMyJoin")
    @ResponseBody
    public Result selMyJoin(HttpServletRequest request, HttpServletResponse response){
        int userid = Integer.parseInt(request.getParameter("id"));
        int page = Integer.parseInt(request.getParameter("pg"));
        List<TbTask> taskList = taskService.selMyJoin(userid,page);
        int count = taskService.selMyJoinCount(userid);
        Result result = new Result();
        if(taskList==null || taskList.size()==0){
            result.setMessage("fail");
        }else{
            result.setMessage("success");
        }
        result.setList(taskList);
        result.setCode(count);
        return result;
    }


    //查新任务详情
    @RequestMapping("selTaskInfo")
    public String selTaskInfo(HttpServletRequest request,HttpServletResponse response)throws Exception{
        System.out.println("aaa");
        String id = request.getParameter("id");
        System.out.println(id+"aaa");
        TbTask tbTask = taskService.selTaskInfo(Integer.parseInt(id));
        if(tbTask != null){
            request.setAttribute("t",tbTask);
            return "details";
        }
        return "index";
    }

    //查新任务详情
    @RequestMapping("getTask")
    @ResponseBody
    public Result getTask(String userid,String taskid){
        System.out.println(userid+"/"+taskid);
        TbUser tbUser = new TbUser(Long.parseLong(userid),null,null,null,null,null,null,null,null,null,null,null,new BigDecimal(0),null,null,null,0,0);
        TbTask tbTask = new TbTask(Long.parseLong(taskid),null,null,null,null,null,null,0,0,0,null,null,0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(df.format(new Date()));
        TbUserTask tbUserTask = new TbUserTask(df.format(new Date()),null,tbUser,tbTask);
        int count = taskService.getTask(tbUserTask);
        Result result = new Result();
        result.setCode(count);
        return result;
    }


}
